﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Core.Log;

namespace Core.Xml.Ogc.Ows
{
    class AddressType : IXmlType
    {
        // TODO: This class is out of the standard stated in OGC OWS page 28(OGC 06-121r9) it is said to be there should be more than one countries or etc. but only one city is allowed.

        public List<string> DeliveryPoints { get; set; }
        public string City { get; set; }
        public string AdministrativeArea { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
        public List<string> ElectronicMailAddresses { get; set; }

        public XElement CreateXElement(XName elementName)
        {
            XElement address = new XElement(elementName);

            bool check = false;

            for (int i = 0; i < DeliveryPoints.Count; i++)
            {
                address.Add(new XElement(Namespaces.OWS + "DeliveryPoint", DeliveryPoints[i]));
                check = true;
            }

            if (!String.IsNullOrEmpty(City))
            {
                address.Add(new XElement(Namespaces.OWS + "City", City));
                check = true;
            }
            if (!String.IsNullOrEmpty(AdministrativeArea))
            {
                address.Add(new XElement(Namespaces.OWS + "AdministrativeArea", AdministrativeArea));
                check = true;
            }
            if (!String.IsNullOrEmpty(PostalCode))
            {
                address.Add(new XElement(Namespaces.OWS + "PostalCode", PostalCode));
                check = true;
            }
            if (!String.IsNullOrEmpty(Country))
            {
                address.Add(new XElement(Namespaces.OWS + "Country", Country));
                check = true;
            }

            for (int i = 0; i < ElectronicMailAddresses.Count; i++)
            {
                address.Add(new XElement(Namespaces.OWS + "ElectronicMailAddress", ElectronicMailAddresses[i]));
                check = true;
            }

            if (!check)
                Logger.GetInstance().Add(new BasicLog(LogPriority.WARNING, "Address Type element is empty, it should be removed."));

            return address;
        }
    }
}
